package com.music.mysongs.fragment.main

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TabHost
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.gson.Gson
import com.music.mysongs.R
import com.music.mysongs.activity.SongActivity
import com.music.mysongs.adapter.CommonRecyclerAdapter
import com.music.mysongs.web.CommonWebTask
import com.music.mysongs.data.*
import com.music.mysongs.file.FileOP
import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.android.synthetic.main.item_recycler_card.view.*
import kotlin.collections.ArrayList

class HomeFragment : Fragment() {
    private val mTAG = "HomeFragment"
    private val codePASS = 10
    private val codeAUDIt = 20

    //定义布局控件
    private var tabHostHome: TabHost? = null
    private var passReView: RecyclerView? = null
    private var auditReView: RecyclerView? = null

    //定义数据
    private var musicPassData: ArrayList<MusicCreatorData.MusicList>? = null
    private var musicAuditData: ArrayList<MusicCreatorData.MusicList>? = null

    private val arrHandler: Handler = Handler {
        when (it.what) {
            codePASS -> {
                passReView!!.adapter = CommonRecyclerAdapter.Builder()
                    .setData(musicPassData!!).setLayout(R.layout.item_recycler_card)
                    .bindView { holder, innerData, position ->
                        val data = innerData as MusicCreatorData.MusicList
                        holder.itemView.txtTitle_item.text = data.title
                        holder.itemView.txtPlayer_item.text = data.artist
                        holder.itemView.txtTitle_item.setOnClickListener {
                            Intent(activity, SongActivity::class.java).apply {
                                putExtra("_pos", position)
                                putExtra("_sort", "web_pass")
                                startActivity(this)
                            }
                        }
                    }.create()
            }
            codeAUDIt -> {
                auditReView!!.adapter = CommonRecyclerAdapter.Builder()
                    .setData(musicAuditData!!).setLayout(R.layout.item_recycler_card)
                    .bindView { holder, innerData, position ->
                        val data = innerData as MusicCreatorData.MusicList
                        holder.itemView.txtTitle_item.text = data.title
                        holder.itemView.txtPlayer_item.text = data.artist
                        holder.itemView.txtTitle_item.setOnClickListener {
                            Intent(activity, SongActivity::class.java).apply {
                                putExtra("_pos", position)
                                putExtra("_sort", "web_audit")
                                startActivity(this)
                            }
                        }
                    }.create()
            }
        }
        false
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        val view: View = inflater.inflate(R.layout.fragment_home, container, false)
        //布局控件赋值
        tabHostHome = view.findViewById(android.R.id.tabhost)
        tabHostHome!!.setup()
        tabHostHome!!.addTab(
            tabHostHome!!.newTabSpec("tab1")
                .setIndicator("已审核", null)
                .setContent(R.id.recyclerViewPass_home)
        )
        tabHostHome!!.addTab(
            tabHostHome!!.newTabSpec("tab2")
                .setIndicator("公示中", null)
                .setContent(R.id.recyclerViewAudit_home)
        )
        passReView = view.findViewById(R.id.recyclerViewPass_home)
        passReView!!.isSaveFromParentEnabled = false
        passReView!!.layoutManager =
            GridLayoutManager(context, 2, GridLayoutManager.VERTICAL, false)
        auditReView = view.findViewById(R.id.recyclerViewAudit_home)
        auditReView!!.layoutManager =
            GridLayoutManager(context, 2, GridLayoutManager.VERTICAL, false)
        return view
    }

    @RequiresApi(Build.VERSION_CODES.M)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initWebData(codePASS, 1)
        initWebData(codeAUDIt, 0)
    }

    //异步获取网络音频数据
    private fun initWebData(reView: Int, isPass: Int) {
        val musicTask = CommonWebTask()
        musicTask.progressBar = progress_home
        musicTask.url = getApiMusicPassOrAuditList(isPass)
        musicTask.setCallbackHandler { innerData ->
            val data = Gson().fromJson(innerData, MusicCreatorData.MusicData::class.java)
            if (isPass == 1) {
                musicPassData = data.data
                FileOP().save(requireContext(), "musicPass.json", innerData)
            } else {
                musicAuditData = data.data
                FileOP().save(requireContext(), "musicAudit.json", innerData)
            }
            val msg = Message()
            msg.what = reView
            arrHandler.sendMessage(msg)
        }
        musicTask.execute()
    }
}